/*
 * Copyright 2012-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.web.client;

import java.net.URI;
import java.util.Map;
import java.util.function.Function;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriTemplateHandler;

/**
 * 用于设置以 {@code '/'} 开头的 URI 根路径的 {@link UriTemplateHandler} 实现。
 *
 * @author Phillip Webb
 * @since 1.4.0
 */
public class RootUriTemplateHandler implements UriTemplateHandler {

	private final String rootUri;

	private final UriTemplateHandler handler;

	protected RootUriTemplateHandler(UriTemplateHandler handler) {
		Assert.notNull(handler, "Handler must not be null");
		this.rootUri = null;
		this.handler = handler;
	}

	/**
	 * 创建新的 {@link RootUriTemplateHandler} 实例。
	 * @param rootUri 用于为相对 URL 添加前缀的根 URI
	 */
	public RootUriTemplateHandler(String rootUri) {
		this(rootUri, new DefaultUriBuilderFactory());
	}

	/**
	 * 创建新的 {@link RootUriTemplateHandler} 实例。
	 * @param rootUri 用于为相对 URL 添加前缀的根 URI，不能为空
	 * @param handler 代理的 URI 模板处理器，不能为空
	 */
	public RootUriTemplateHandler(String rootUri, UriTemplateHandler handler) {
		Assert.notNull(rootUri, "RootUri must not be null");
		Assert.notNull(handler, "Handler must not be null");
		this.rootUri = rootUri;
		this.handler = handler;
	}

	@Override
	public URI expand(String uriTemplate, Map<String, ?> uriVariables) {
		return this.handler.expand(apply(uriTemplate), uriVariables);
	}

	@Override
	public URI expand(String uriTemplate, Object... uriVariables) {
		return this.handler.expand(apply(uriTemplate), uriVariables);
	}

	private String apply(String uriTemplate) {
		if (StringUtils.startsWithIgnoreCase(uriTemplate, "/")) {
			return getRootUri() + uriTemplate;
		}
		return uriTemplate;
	}

	public String getRootUri() {
		return this.rootUri;
	}

	/**
	 * 从当前实例派生一个新的 {@code RootUriTemplateHandler}，通过给定的 {@code wrapper} 包装其代理的
	 * {@link UriTemplateHandler}。
	 * @param wrapper 要应用于代理 URI 模板处理器的包装器
	 * @return 新的处理器实例
	 * @since 2.3.10
	 */
	public RootUriTemplateHandler withHandlerWrapper(Function<UriTemplateHandler, UriTemplateHandler> wrapper) {
		return new RootUriTemplateHandler(getRootUri(), wrapper.apply(this.handler));
	}

	/**
	 * 将一个 {@link RootUriTemplateHandler} 实例添加到给定的 {@link RestTemplate}。
	 * @param restTemplate 需要添加处理器的 {@link RestTemplate}
	 * @param rootUri 根 URI
	 * @return 添加的 {@link RootUriTemplateHandler} 实例
	 */
	public static RootUriTemplateHandler addTo(RestTemplate restTemplate, String rootUri) {
		Assert.notNull(restTemplate, "RestTemplate must not be null");
		RootUriTemplateHandler handler = new RootUriTemplateHandler(rootUri, restTemplate.getUriTemplateHandler());
		restTemplate.setUriTemplateHandler(handler);
		return handler;
	}

}
